define([
   /* 'views/home/widgetEditor',
    'views/home/widgets/lastDashboards',
    'views/home/widgets/lastModels',
    'views/home/widgets/tilesArea',
    'views/home/widgets/myReports',
    'controllers/home/pluginManager',
    'text!templates/home/selectorMenuItem.html'*/
], function (/*WidgetEditor, LastDashboardsView, LastModelsView, TilesAreaView, MyReportsView, PluginController, ItemTemplate*/) {

    var controller = BackboneMVC.Controller.extend({
        name: 'home',
        taskInitied: false,
        sections: [{ key: "summary", title: "Summary" }],
        showApp: function () {
            require(['views/home/mainApp'], function (MainHomeView) {
                var mainHomeView = new MainHomeView();
                mainHomeView.render();
            });
        },
        
        showSidebar: function () {
            require(['controllers/sidebar/sidebarMngr'], function (Controller) {
                (new Controller()).show();
            });
        },

        showMainDashboard: function () {
            require(['views/home/mainDashboard'], function (View) {
                (new View()).render();
            });
        },

        initializeTasks: function () {
            if (!this.taskInitied) {
                this.taskInitied = true;
                var _this = this;
                $("#topMenuBar ul").on('click', "a", function (event) {
                    _this.selectTask(($(this).attr("rel")));
                });

                $("#topMenuBar a.btn-remove").click(function (event) {
                    var key = $("#currentTask").attr("data-rel");
                    if (key != "summary")
                        _this.removeTask(key);
                });

            }
        },
        existsTask: function (key) {
            var res = false;
            for (var nn = 0; nn < this.sections.length; nn++) {
                if (this.sections[nn].key == key) {
                    res = true;
                    break;
                }
            }
            return res;
        },
        selectTask: function (key) {
            $("#main div.mainTask").hide();
            $("#main div[data-rel='" + key + "']").show();
            $("#currentTask").html($("#topMenuBar ul").find("li a[rel='" + key + "']").html());
            $("#currentTask").attr("data-rel", key);
            $("#topMenuBar a.btn-remove").hide();
            if (key != "summary")
                $("#topMenuBar a.btn-remove").show();

            // solve chrome bug
            $(window).trigger("resize");

            $("#main div[data-rel='" + key + "']").trigger("selectView")

        },
        hideAllTasks: function () {
            $("#main div.mainTask").hide();
        },
        addSimpleTask: function (key, title) {
            this.initializeTasks();
            var current;
            for (var nn = 0; nn < this.sections.length; nn++) {
                if (this.sections[nn].key == key) {
                    current = this.sections[nn];
                    break;
                }
            }
            if (!current) {
                var item = { key: key, title: title };
                this.sections.push(item);

                // put html to selector
                var templateItem = Handlebars.compile(ItemTemplate);
                var html = templateItem(item);
                $("#topMenuBar ul").append(html);
            }
            return current;
        },
        addTask: function (key, title) {
            if (key) {
                var current = this.addSimpleTask(key, title);
                if (!current)
                    current = this.addView(key);
                if (current) {
                    this.hideAllTasks();
                    this.selectTask(key);
                }
            }
        },
        //addView: function (key, element) {
        //    var added;
        //    switch (key) {
        //        case "map":
        //            require(['controllers/map/mapManager'], function (Controller) {
        //                (new Controller()).show(element);
        //            });
        //            added = true;
        //            break;
        //        case "session-manager":
        //            require(['controllers/security/sessions'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "abm-company":
        //            require(['controllers/abmManagers/companyManager'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "abm-rol":
        //            require(['controllers/abmManagers/rolManager'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "user_mngr":
        //            require(['controllers/crud/userMngr'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "abm-group":
        //            require(['controllers/abmManagers/groupManager'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "abm-preference":
        //            require(['controllers/abmManagers/preferenceManager'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;

        //        default:
        //            added = (new PluginController()).addView(key)
        //            break;
        //    }
        //    return added;
        //},
        //addView: function (key) {
        //    var added;
        //    switch (key) {
        //        case "file-manager":
        //            require(['controllers/fileManager/fileManager'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "session-manager":
        //            require(['controllers/security/sessions'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "abm-company":
        //            require(['controllers/abmManagers/companyManager'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "abm-rol":
        //            require(['controllers/abmManagers/rolManager'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "abm-user":
        //            require(['controllers/abmManagers/userManager'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "abm-group":
        //            require(['controllers/abmManagers/groupManager'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;
        //        case "abm-preference":
        //            require(['controllers/abmManagers/preferenceManager'], function (Controller) {
        //                (new Controller()).show();
        //            });
        //            added = true;
        //            break;

        //        default:
        //            added = (new PluginController()).addView(key)
        //            break;
        //    }
        //    return added;
        //},
        removeTask: function (key) {
            this.selectTask("summary");

            $("#topMenuBar ul li a[rel='" + key + "']").parent().remove();
            $("#main div[data-rel='" + key + "']").trigger("removeView")
            $("#main div[data-rel='" + key + "']").empty();
            $("#main div[data-rel='" + key + "']").remove();

            //remove section
            for (var nn = 0; nn < this.sections.length; nn++) {
                if (this.sections[nn].key == key) {
                    this.sections.splice(nn);
                    break;
                }
            }
        },
        removeOtherTask: function (tasks) {
            tasks.push("summary");
            for (var nn = this.sections.length - 1; nn >= 0; nn--) {

                if (tasks.indexOf(this.sections[nn].key) == -1) {
                    var key = this.sections[nn].key;
                    this.removeTask(key);
                }
            }
        }
    });

    return controller;

});